<template>
  <div style="background-color: #f4f4f8; height: 100vh">
    <van-nav-bar
      class="top"
      :title="subject"
      left-text=""
      right-text=""
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    />

    <div v-if="chapterList.length != 0" class="isbody">
      <div
        v-loading="loading"
        class="body_item"
        @click="goKnowlage(item)"
        v-for="item of chapterList"
        :key="item.id"
      >
        <img v-if="item.surface" :src="item.surface" alt="" />
        <img v-else src="@/assets/kkk.png" alt="" />
        <p>{{ item.chapter_title }}</p>
      </div>
    </div>
    <van-empty v-else description="暂无知识点" />
  </div>
</template>

<script>
import { chapter, exerList } from "@/api/admin.js";
import { mapGetters } from "vuex";
import {
  getsubjectTitle,
  getsubject_id,
  getapplyid,
  getisfacetime,
  getisfacetimezxface,
} from "@/utils/auth.js";
export default {
  computed: {
    ...mapGetters(["userinfor"]),
  },
  data() {
    return {
      loading: false,
      daan: "不显示",
      gz: "",
      subject: "",
      chapterList: [],
      restart: 0,
      applyid: getapplyid(),
    };
  },
  methods: {
    goKnowlage() {
      this.loading = true;
      let face = "";
      if (getisfacetime()) {
        face = JSON.parse(getisfacetime()).face;
      } else if (getisfacetimezxface()) {
        face = JSON.parse(getisfacetimezxface()).face;
      }
      let form = {
        subjectid: this.subjectid,
        gz: this.gz,
        qtype: "",
        // pdtx: JSON.parse(JSON.stringify(this.pdtx)).join(),
        answer: "",
        form: "知识点模式",
        restart: this.restart,
        applyid: this.applyid,
        pool: "练习题库",
        face: face,
      };
      exerList(form).then((res) => {
        if (res.code == 200) {
          if (
            res.data.status == "题库中没有试题" ||
            res.data.status == "没有错题"
          ) {
            this.$toast.fail(res.data.status);
          }
          if (res.data.status == "已做完") {
            this.$dialog
              .confirm({
                title: "提示",
                message: "题库试题已做完，是否重新开始做题",
                showCancelButton: true,
              })
              .then(() => {
                form.restart = 1;
                form.face = getisfacetimezxface()
                  ? JSON.parse(getisfacetimezxface()).face
                  : "";
                exerList(form).then((resA) => {
                  if (resA.code == 200) {
                    this.$store.commit("SET_TI", resA.data.list);
                    form.restart = 0;
                    this.$router.push(
                      "/exam?exerid=" +
                        resA.data.exerid +
                        "&subjectid=" +
                        this.subjectid +
                        "&daan=" +
                        this.daan +
                        "&form=" +
                        JSON.stringify(form) +
                        "&progress=" +
                        resA.data.progress
                    );
                  }
                });
              });
          }
          if (res.data.status == "正常") {
            form.restart = 0;
            // this.$store.dispatch('questions/commitTi',res.data.list)
            this.$store.commit("SET_TI", res.data.list);
            this.loading = false;
            this.$router.push(
              "/exam?exerid=" +
                res.data.exerid +
                "&subjectid=" +
                this.subjectid +
                "&daan=" +
                this.daan +
                "&form=" +
                JSON.stringify(form) +
                "&progress=" +
                res.data.progress
            );
          }
        } else {
          this.loading = false;
          this.$toast.fail(res.msg);
        }
      });
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    onClickRight() {},
    getList() {
      chapter({
        subject_id: this.subjectid,
        limit: 30,
      }).then((res) => {
        if (res.code == 200) {
          this.chapterList = res.data.list;
        }
      });
    },
  },
  created() {
    this.subjectid = getsubject_id();
    this.subject = getsubjectTitle();
    this.gz = this.userinfor.filter(
      (item) => item.subject_id == getsubject_id()
    )[0].gz;
    this.getList();
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .isbody {
    margin: 0.93rem 0.75rem 0;
    padding: 0.93rem 0.75rem 0;
    min-height: 84vh;
    height: auto;
    background: #fff;
    border-radius: 8px;
    .body_item {
      margin-bottom: 10px;
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
      img {
        width: 6.25rem;
        height: 4.125rem;
        border-radius: 8px;
        margin-right: 0.75rem;
      }
      p {
        font-size: 0.93rem;
        font-weight: 600;
      }
    }
  }
  .van-icon {
    color: #000;
  }
}
.header {
  height: 2.75rem;
  width: 100%;
  background: #fff;
}
</style>